const express = require('express');
const db = require('../utils/database');
const myRouter = express.Router();

myRouter.get('/get/onlineShop', (req, res) => {
    const count = req.query.count;
    const page = req.query.page;
    const sql1 = `SELECT * FROM z_sale 
    LEFT JOIN production ON z_sale.p_id=production.p_id
    LEFT JOIN out_stock ON production.p_ck_id=out_stock.out_stock_id
    LEFT JOIN in_stock ON in_stock.in_stock_id=out_stock.rk_id
    LEFT JOIN transport ON in_stock.trans_id=transport.t_id
    LEFT JOIN house ON transport.t_warehouse=house.house_id
    LEFT JOIN tran_person ON transport.t_person=tran_person.p_id
    LEFT JOIN purchase ON transport.t_shopid=purchase.id
    LEFT JOIN goods ON purchase.p_item=goods.g_id
    LEFT JOIN class2 ON class2.c2_id=goods.g_class
    LEFT JOIN class1 ON class1.c1_id=class2.class1_id
    WHERE 销售方式='网购' LIMIT ${(page - 1) * count},${count}`;
    const sql2 = `SELECT COUNT(*) total FROM z_sale 
    LEFT JOIN production ON z_sale.p_id=production.p_id
    LEFT JOIN out_stock ON production.p_ck_id=out_stock.out_stock_id
    LEFT JOIN in_stock ON in_stock.in_stock_id=out_stock.rk_id
    LEFT JOIN transport ON in_stock.trans_id=transport.t_id
    LEFT JOIN house ON transport.t_warehouse=house.house_id
    LEFT JOIN tran_person ON transport.t_person=tran_person.p_id
    LEFT JOIN purchase ON transport.t_shopid=purchase.id
    LEFT JOIN goods ON purchase.p_item=goods.g_id
    LEFT JOIN class2 ON class2.c2_id=goods.g_class
    LEFT JOIN class1 ON class1.c1_id=class2.class1_id
    WHERE 销售方式='网购'`;
    db.query(sql1, (err1, data1) => {
        if (err1) {
            res.send('截取数据时出错')
        } else {
            db.query(sql2, (err2, data2) => {
                if (err2) {
                    res.send('查询总条数时出错')
                } else {
                    res.send({ data: data1, total: data2[0].total })
                    
                }
            })
        }
    })
})

module.exports = myRouter;